﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ThousandFunClient.model;
using ThousandFunClient.Util;
using System.Collections;
using System.IO.Ports;
using System.Timers;
using log4net;

namespace ThousandFunClient.Util
{
    class DataUtil
    {
        //返回true表示通过检验
        public static bool verifyData(byte[] buf)
        {

            if (true || false)
            {
                int i = 5;
            }
            if(!buf[0].Equals(0)||buf.Length<6)
            {
                return false;
            }
            return true;
        }

        public static byte[] convertSendDataBytes(String CMD)
        {
            int[] bs = convertSendDataInt(CMD);
            byte[] bsb = new byte[bs.Length];
            for (int i = 0; i < bs.Length; i++)
            {

                bsb[i] = (byte)bs[i];
            }
            return bsb;
        }
        public static int[] convertSendDataInt(String CMD)
        {
            String[] tc = CMD.Split(' ');
            int[] tg = new int[tc.Length];
            for (int i = 0; i < tc.Length; i++)
            {
                tg[i] = Convert.ToInt32(tc[i], 16);
            }


            return tg;
        }


        public static String[] convertSendData(String CMD)
        {
            String[] tc = CMD.Split(' ');
            String[] tg = new String[tc.Length];
            for (int i = 0; i < tc.Length; i++)
            {
                tg[i] = Convert.ToInt32(tc[i], 16).ToString();
            }


            return tg;
        }

        public static String convertSendDataStr(String CMD)
        {
            String[] tg = convertSendData(CMD);
            string ret = "";
            for (int i = 0; i < tg.Length; i++)
            {
                ret = ret + tg[i];
            }

            return ret;
        }

        public static TFModel parseReceiveint(byte[] realyrec)
        {
            
            TFModel tfmodel = new TFModel();
            tfmodel.Startbyte = fillhex(inttohex((int)realyrec[0]));
            tfmodel.LOProtocol = fillhex(inttohex((int)realyrec[1]));
            tfmodel.DOCommand = fillhex(inttohex((int)realyrec[2]));
            tfmodel.SOCommand = fillhex(inttohex((int)realyrec[3]));
            tfmodel.Separator = fillhex(inttohex((int)realyrec[4]));
            tfmodel.DataType = fillhex(inttohex((int)realyrec[5]));

            
            int len = Int32.Parse(tfmodel.LOProtocol);
            //目前先按照data长度等于总长度减4处理
            int datalen = len - 4;

            for (int i = 6 ; i < 6 + datalen; i++)
            {
                tfmodel.AnswerdataLog = tfmodel.AnswerdataLog + fillhex(inttohex((int)realyrec[i]));
            }

            for (int i = 5 + datalen; i > 5; i--)
            {
                tfmodel.Answerdata = tfmodel.Answerdata + fillhex(inttohex((int)realyrec[i]));
            }
            tfmodel.Checksum = inttohex((int)realyrec[5 + datalen + 1]);

            return tfmodel;
        }

        private static string fillhex(string source)//主要方法
        {
            if (source.Length < 2)
            {
                return "0" + source;
            }
            else
            {
                return source;
            }
        }

        public static TFModel parseReceive(String receive)
        {
            String[] realyrec = receive.Split(' ');
            TFModel tfmodel = new TFModel();
            tfmodel.Startbyte = realyrec[0];
            tfmodel.LOProtocol = realyrec[1];
            tfmodel.DOCommand = realyrec[2];
            tfmodel.SOCommand = realyrec[3];
            tfmodel.Separator = realyrec[4];
            tfmodel.DataType = realyrec[5];

            int len = Int32.Parse(tfmodel.LOProtocol);
            int cc = tfmodel.SOCommand.Length;
            //目前先按照data长度等于总长度减4处理
            int datalen = len -4;
            for (int i = 5+datalen; i > 5; i--)
            {
                tfmodel.Answerdata = tfmodel.Answerdata + realyrec[i];
            }
            tfmodel.Checksum = realyrec[5 + datalen + 1];

            return tfmodel;
        }
        private static string specinttostring(int source)//被主要方法调用的一个辅助方法
        {
            if (source < 10)
            {
                return source.ToString();
            }
            else
            {
                switch (source)
                {
                    case 10:
                        return "a";
                    case 11:
                        return "b";
                    case 12:
                        return "c";
                    case 13:
                        return "d";
                    case 14:
                        return "e";
                    case 15:
                        return "f";
                    default:
                        return "";
                }
            }
        }


        private static string inttohex(int source)//主要方法
    {
        if(source <10)
        {
            return source.ToString();
        }
        else if (source <=15)
        {
            return specinttostring(source);
        }
        else
        {
            int raisenum = 16;
            int addnum = 16;
            int positionnum = 1;
            while((source - addnum) >= 0)
            {
                positionnum++;
                addnum = addnum * raisenum;
            }
            int[] valuepositionnum = new int[positionnum];
            for(int i = 0;i<positionnum;i++)
            {
                valuepositionnum[i] = 0;
            }

            int[] valueaddnum = new int[positionnum];

            for(int i = 0;i<positionnum;i++) 
            {
                valueaddnum[i] = Convert.ToInt32(Math.Pow(raisenum,i));
            }

int[] decreasesource = new int[positionnum];

decreasesource[positionnum -1] = source;

for(int i = positionnum -1;i>=0;i--)

{

while((decreasesource[i] - valueaddnum[i] ) >=0)

{

if(i != 0)

decreasesource[i -1] = decreasesource[i] - valueaddnum[i] ;

valuepositionnum[i]++;

valueaddnum[i] = valueaddnum[i] + Convert.ToInt32(Math.Pow(raisenum, i));

}

}

string[] stringvaluepositionnum = new string[positionnum];

for (int i = 0; i < positionnum; i++) 

{

stringvaluepositionnum[i] = specinttostring(valuepositionnum[i]);

}

string result = "";

for(int i = positionnum -1;i>=0;i--)

{

result = result + stringvaluepositionnum[i];

}

return result;
}

}

    }
}
